package cn.westv.scps.web.listener;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.westv.scps.domain.AreaSeverEntity;
import cn.westv.scps.domain.SysConfig;
import cn.westv.scps.mqtt.ClientMQTT;
import cn.westv.scps.mqtt.PushCallback;
import cn.westv.scps.service.areaserver.IAreaServerService;
import cn.westv.scps.service.sys.config.ISysConfigService;
import cn.westv.scps.utils.SpringContextUtil;
import cn.westv.scps.utils.StringUtils;
import cn.westv.scps.weather.WeatherTools;

public class SpringLoaderListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		Logger log = Logger.getLogger(SpringLoaderListener.class);
		log.info("服务器启动成功！开始数据初始化。。。");
		ServletContext context = sce.getServletContext();
		WebApplicationContext webContext = WebApplicationContextUtils.getWebApplicationContext(context);
		SpringContextUtil.setApplicationContextStaticlly(webContext);

		// ----------------------------刷新天气状态线程--------------------------------
//		weatherRefreshThread();
		// ----------------------------刷新天气状态线程--------------------------------

		
		//-----------------------------MQTT启动
		

		IAreaServerService areaServerService = (IAreaServerService) SpringContextUtil.getBean("areaServerServiceImpl");
		List<AreaSeverEntity> serverList = areaServerService.find("from AreaSeverEntity", AreaSeverEntity.class, null);
		// 启动mqtt服务
		for (final AreaSeverEntity areaServer : serverList) {
			if (StringUtils.isNotBlank(areaServer.getMqttHostIp())) {
				final String mqttHost = "tcp://" + areaServer.getMqttHostIp() + ":" + areaServer.getMqttTcpHostPort();
//				Logger log = Logger.getLogger(SpringLoaderListener.class);
				log.info("mqtt启动:服务器名称是：" + areaServer.getServerName() + ",mqtt服务器是host:" + mqttHost);
				Runnable run = new Runnable() {
					@Override
					public void run() {
						ClientMQTT client = new ClientMQTT(mqttHost, areaServer.getMqttAccount(),
								areaServer.getMqttPwd());
						client.start();

					}
				};
				Thread t = new Thread(run);
				t.start();
			}
		}
		
		//------------------------------
		
		
		
		
		
		// ---------Mqtt消息去重复内容初始化处理----------------------
		try {
			PushCallback.RWmqttMsgSetFile(null, PushCallback.getMqttMsgFile(), "read");
		} catch (Exception e) {
			ByteArrayOutputStream out = null;
			try {
				out = new ByteArrayOutputStream();
				PrintStream s = new PrintStream(out);
				e.printStackTrace(s);
				String string = new String(out.toByteArray());
				log.error(string);
				log.debug(string);
			} catch (Exception e1) {
				e1.printStackTrace();
			} finally {
				if (out != null) {
					try {
						out.close();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			}

		}
		// ---------Mqtt消息去重复内容初始化处理----------------------
	}

	/**
	 * 天气状态刷新线程
	 */
	private void weatherRefreshThread() {

		Runnable weatherRunnable = new Runnable() {
			@Override
			public void run() {
				WeatherTools wt = new WeatherTools();
				Logger log = Logger.getLogger(SpringLoaderListener.class);
				log.info("刷新天气线程启动。。。。。");
				try {
					wt.refreshWeather();// 刷新天气集合
				} catch (Exception e) {
					e.printStackTrace();
				}
				log.info(new Date() + "执行了一次刷新天气。。。。");
			}

		};

		Thread weatherThread = new Thread(weatherRunnable);
		weatherThread.start();

	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub

	}

}
